/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.BuildingTypeForm;
import com.ntb.entities.BuildingTypes;
import com.ntb.models.BuildingTypesDAO;
import com.ntb.models.impl.BuildingTypesDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class BuildingTypeAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
                
        ActionForward forward = mapping.getInputForward();
        BuildingTypeForm buildingTypeForm = (BuildingTypeForm) form;
        BuildingTypesDAO buildingTypeDao = new BuildingTypesDAOImpl();
        String action = buildingTypeForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        if ("save".equalsIgnoreCase(action)) {
            BuildingTypes buildingType = new BuildingTypes();

            buildingType.setBuildingTypeID(buildingTypeForm.getBuildingTypeID());
            buildingType.setBuildingTypeName(buildingTypeForm.getTypeName());

            if (buildingType.getBuildingTypeID() > 0) {
                BuildingTypes temp = buildingTypeDao.getRow(buildingType.getBuildingTypeID());
                if (temp == null) {
                    errorMessages.add("Building type not found!");
                } else {
                    boolean available = true;

                    if (!temp.getBuildingTypeName().equalsIgnoreCase(buildingType.getBuildingTypeName())) {
                        if (buildingTypeDao.checkDuplicateName(buildingType.getBuildingTypeName())) {
                            available = false;
                            errorMessages.add("The building type name you selected is already in use.");
                            forward = mapping.findForward("buildingTypeEdit");
                        }
                    }

                    if (available) {
                        buildingTypeDao.update(buildingType);
                        successMessages.add("The building type \"" + buildingType.getBuildingTypeName() + "\" has been saved.");
                        forward = mapping.findForward("buildingTypeListing");
                    }
                }
            } else {
                boolean availale = true;
                if (buildingTypeDao.checkDuplicateName(buildingType.getBuildingTypeName())) {
                    availale = false;
                    errorMessages.add("The building type name you selected is already in use.");
                }

                if (availale) {
                    buildingTypeDao.add(buildingType);
                    successMessages.add("The building type \"" + buildingType.getBuildingTypeName() + "\" has been added.");
                    forward = mapping.findForward("buildingTypeListing");
                }
            }
        } else if (buildingTypeForm.getBuildingTypeID() > 0) {
            BuildingTypes buildingType = buildingTypeDao.getRow(buildingTypeForm.getBuildingTypeID());

            buildingTypeForm.setBuildingTypeID(buildingType.getBuildingTypeID());
            buildingTypeForm.setTypeName(buildingType.getBuildingTypeName());

            forward = mapping.findForward("buildingTypeEdit");
        } else {
            buildingTypeForm.setBuildingTypeID(0);
        }

        request.setAttribute("errorMessages", errorMessages);
        request.setAttribute("successMessages", successMessages);

        return forward;
    }

}
